package com.smile.service.handler;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.smile.mapper.OrganizationMapper;
import com.smile.model.domain.SmilePlusOrganization;
import com.smile.service.config.mqtt.handler.AbstractAirportOrganizationGetHandler;
import com.smile.service.entity.organization.AirportOrganizationInfo;
import com.smile.service.entity.organization.AirportOrganizationResult;
import com.smile.service.entity.response.DeviceConfigResponse;
import lombok.extern.slf4j.Slf4j;
import org.eclipse.paho.client.mqttv3.MqttClient;
import org.eclipse.paho.client.mqttv3.MqttConnectOptions;
import org.eclipse.paho.client.mqttv3.MqttException;
import org.eclipse.paho.client.mqttv3.MqttMessage;
import org.eclipse.paho.client.mqttv3.persist.MemoryPersistence;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Slf4j
public class DefaultAirportOrganizationGetHandler extends AbstractAirportOrganizationGetHandler {
    @Autowired
    private OrganizationMapper organizationMapper;
    @Override
    public Boolean process(String data, String tid, String bid,String gateway) throws MqttException {

        AirportOrganizationInfo airportOrganizationInfo = JSONObject.parseObject(data, AirportOrganizationInfo.class);
        QueryWrapper<SmilePlusOrganization> queryWrapper1 = new QueryWrapper<>();
        System.out.println(airportOrganizationInfo);
        queryWrapper1.eq("device_binding_code", airportOrganizationInfo.getDeviceBindingCode());
        System.out.println(airportOrganizationInfo.getOrganizationId());
        System.out.println(airportOrganizationInfo.getDeviceBindingCode());
        if(airportOrganizationInfo.getOrganizationId().equals("")){

        }else {
            queryWrapper1.eq("organization_code", airportOrganizationInfo.getOrganizationId());
        }

        SmilePlusOrganization organization = organizationMapper.selectOne(queryWrapper1);
        System.out.println(JSONObject.toJSONString(organization));
        AirportOrganizationResult airportOrganizationResult = new AirportOrganizationResult();
        airportOrganizationResult.setResultCode(0);
        AirportOrganizationResult.OrganizationOutput organizationOutput = new AirportOrganizationResult.OrganizationOutput();
        organizationOutput.setOrganizationName(organization.getOrganizationName());
        airportOrganizationResult.setOutput(organizationOutput);
        airportOrganizationResult.setResultCode(0);

        DeviceConfigResponse<AirportOrganizationResult> deviceConfigResponse = new DeviceConfigResponse<>();
        deviceConfigResponse.setBid(bid);
        deviceConfigResponse.setTid(tid);
        deviceConfigResponse.setData(airportOrganizationResult);

        deviceConfigResponse.setTimestamp(System.currentTimeMillis());
        deviceConfigResponse.setMethod("airport_organization_get");
        deviceConfigResponse.setGateway(gateway);
        System.out.println("DefaultAirportOrganizationGetHandler发送的结果为:" + JSONObject.toJSONString(deviceConfigResponse));
        log.info("DefaultAirportOrganizationGetHandler {}", com.alibaba.fastjson2.JSONObject.toJSONString(deviceConfigResponse));

        MqttMessage message = new MqttMessage();
        message.setQos(0);
        message.setRetained(true);
        message.setPayload(JSONObject.toJSONString(deviceConfigResponse).getBytes());
        MqttClient mqttClient = new MqttClient("tcp://120.211.119.131:1883", "feisuan-mqtt-producer-client", new MemoryPersistence());
        MqttConnectOptions connOpts = new MqttConnectOptions();
        connOpts.setUserName("feisuan");
        connOpts.setPassword("feisuan123456".toCharArray());
        mqttClient.connect(connOpts);

        mqttClient.publish("thing/product/" + gateway + "/requests_reply", message);

        return Boolean.TRUE;
    }
}
